<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-row style="height:60px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="show=true">新增角色</el-button>
            </el-row>
            <!-- //按钮 -->

            <!-- 表格 -->
            <el-table border :data='list'>
              <el-table-column label="序号" width="120" type=index align='center'></el-table-column>
              <el-table-column label="角色名称" width="240" prop="name" align='center'></el-table-column>
              <el-table-column label="描述" prop="description" align='center'></el-table-column>
              <el-table-column label="操作" align='center'>
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- //表格 -->

            <!-- 分页 -->
            <el-row type="flex" justify="center" style="height:60px" align="middle">
              <el-pagination layout="prev, pager, next" :current-page='page.page' :page-count='page.total'
                :page-size='page.pagesize' @current-change='changePage'>
              </el-pagination>
            </el-row>
            <!-- //分页 -->
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <el-alert title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" type="info" show-icon :closable="false">
            </el-alert>
            <!-- 公司名称表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input disabled style="width:400px" v-model="fromDate.name"></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input disabled style="width:400px" v-model="fromDate.companyAddress"></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input disabled style="width:400px" v-model="fromDate.mailbox"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input disabled style="width:400px" type="textarea" :rows="3" v-model="fromDate.remarks"></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>
    <!-- 编辑弹层 -->
    <el-dialog :visible='show' title="编辑弹层" @close='btnCancel'>
      <el-form :model="roleForm" :rules="rules" ref="roleForm">
        <el-form-item label="角色名称" label-width="120px" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" label-width="120px" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确认</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <!-- //分配权限弹层 -->
    <el-dialog :visible.sync='showPermDialog' title="分配权限">
      <el-tree :data="permData" :props="defaultProps" default-expand-all node-key='id' :check-strictly='true'
        :default-checked-keys='selectCheck' show-checkbox ref="permTree"></el-tree>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnpermOk">确定</el-button>
          <el-button size="small" @click="btnpermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
import { mapGetters } from 'vuex'
export default {
  data () {
    return {
      //列表数据
      list: [],
      //页码查询参数
      page: {
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      },
      //公司信息
      fromDate: {},
      //控制弹层显示
      show: false,
      //角色表单
      roleForm: {
        name: '',
        description: ''
      },
      //角色表单校验
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      //权限点数据
      permData: [],
      //分配权限弹层
      showPermDialog: false,
      // tree默认显示的
      defaultProps: {
        label: 'name'
      },
      //存储选择的员工id
      roleId: '',
      //用户拥有的权限数据
      selectCheck: []
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created () {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo() //获取公司信息
  },
  methods: {
    //获取列表数据
    async getRoleList () {
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    //获取企业信息
    async getCompanyInfo () {
      this.fromDate = await getCompanyInfo(this.companyId)
    },
    //点击删除
    async deleteRole (id) {
      try {
        await this.$confirm('确认删除该角色?')
        await deleteRole(id)
        this.$message.success('删除成功')
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    //点击编辑
    async editRole (id) {
      //角色数据回写
      this.roleForm = await getRoleDetail(id)
      //打开弹层
      this.show = true
    },
    //点击弹层确认
    async btnOk () {
      //校验表单
      try {
        await this.$refs.roleForm.validate()
        //校验通过才会进入这
        //如果有id那么就是修改
        if (this.roleForm.id) {
          // 提交修改的信息
          await updateRole(this.roleForm)
        } else {
          //没有id就是新增
          await addRole(this.roleForm)
        }
        this.getRoleList()
        this.$message.success('操作成功')
        this.show = false
      } catch (error) {
        console.log(error)
      }
    },
    //点击取消按钮
    btnCancel () {
      //清空表单数据
      //角色表单
      this.roleForm = {
        name: '',
        description: ''
      },
        //移除校验
        this.$refs.roleForm.resetFields()
      //关闭弹层
      this.show = false
    },
    //分页的页码值改变
    changePage (newPage) {
      this.page.page = newPage
      // 获取角色列表
      this.getRoleList()
    },
    //点击分配权限按钮
    async assignPerm (id) {
      this.roleId = id
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      //回写用户拥有的权限id
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPermDialog = true
    },
    //分配权限确定按钮
    async btnpermOk () {
      //getCheckedKeys被选择的id形成的数组  因为组件绑定的key是id
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('角色分配成功')
      this.showPermDialog = false
    },
    //分配权限取消按钮
    btnpermCancel () {
      this.selectCheck = []
      this.showPermDialog = false
    }
  }
}
</script>

<style>
</style>
